﻿using Caliburn.Micro;
using KouDai.Phone.Engine;
using KouDai.Phone.Helper;
using KouDai.Phone.Models;
using KouDai.Phone.Services;
using KouDai.Phone.Utils;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;

namespace KouDai.Phone.ViewModels
{
    public class RankingListViewModel : KouDaiScreen
    {
        #region Fields
        private string _startDate = "0";
        private readonly INavigationService _navigationService;
        private ObservableCollection<RankingListEntity> _items = new ObservableCollection<RankingListEntity>();
        private bool _releaseShow = false;
        #endregion

        #region Constructor
        public RankingListViewModel()
        {
            if (Execute.InDesignMode)
            {
                LoadDesignData();
            }
        }

        public RankingListViewModel(INavigationService navigationService)
        {
            _navigationService = navigationService;
        }
        #endregion

        #region Properties
        public ObservableCollection<RankingListEntity> Items
        {
            get { return _items; }
            set
            {
                if (value != _items)
                {
                    _items = value;
                    this.NotifyOfPropertyChange(() => this.Items);
                }
            }
        }
        public string CombineAppId { get; set; }

        public bool ReleaseShow
        {
            get { return _releaseShow; }
            set
            {
                if (value != _releaseShow)
                {
                    _releaseShow = value;
                    this.NotifyOfPropertyChange(() => this.ReleaseShow);
                }
            }
        }
        #endregion

        #region Override
        public override void LoadDesignData()
        {
            List<RankingListEntity> rankingList = new List<RankingListEntity>();
            rankingList.Add(new RankingListEntity()
            {
                Date = "05月17日",
                LatestUpdateTime = 1337184000000,
                UpdateTimeTip = "20:00更新",
                Week = "星期四",
                HoneyItems = new ObservableCollection<HoneyEntity>
                            {
                                new HoneyEntity(){FaviorateAmout=466,ImageUrlForIphone="http://img.geilicdn.com/taobao13813024933_146x146.jpg",ID="taobao13813024933"},
                                new HoneyEntity(){FaviorateAmout=458,ImageUrlForIphone="http://img.geilicdn.com/taobao17219460721_146x146.jpg",ID="taobao17219460721"},
                                new HoneyEntity(){FaviorateAmout=425,ImageUrlForIphone="http://img.geilicdn.com/taobao14531473910_146x146.jpg",ID="taobao14531473910"},
                                new HoneyEntity(){FaviorateAmout=421,ImageUrlForIphone="http://img.geilicdn.com/taobao17270972387_146x146.jpg",ID="taobao17270972387"},
                                new HoneyEntity(){FaviorateAmout=405,ImageUrlForIphone="http://img.geilicdn.com/taobao14678566598_146x146.jpg",ID="taobao14678566598"},
                                new HoneyEntity(){FaviorateAmout=396,ImageUrlForIphone="http://img.geilicdn.com/taobao14453269505_146x146.jpg",ID="taobao14453269505"}
                            }
            });
            rankingList.ToObservableCollection<RankingListEntity>(this.Items);
        }

        protected override void OnInitialize()
        {
            GetRankingList();
        }
        #endregion

        #region Method
        public async void GetRankingList()
        {
            BackData backData = await ServiceManager.GetRankingList(_startDate, CombineAppId);
            if (backData.IsSuccess)
            {
                List<RankingListEntity> rankingList = backData.ResposeData  as List<RankingListEntity>;
                rankingList.ToObservableCollection(this.Items);
                _startDate = rankingList[0].LatestUpdateTime.ToString();
            }
            else
            {
                Debug.WriteLine(backData.Msg);
            }
        }
        #endregion

        #region ViewMethod
        public void RankSelectExecute(HoneyEntity selectedItem)
        {
            Debug.WriteLine("url" + selectedItem.ImageUrlForIphone.ToString());
        }

        public void PullDownRefresh()
        {
            _startDate = "0";
            this.Items.Clear();
            GetRankingList();
        }
        #endregion

    }
}
